package com.example.pilipili.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.pilipili.common.R;
import com.example.pilipili.dao.UserMapper;
import com.example.pilipili.pojo.dto.VInfo;
import com.example.pilipili.pojo.entity.Video;
import com.example.pilipili.dao.VideoMapper;
import com.example.pilipili.service.VideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.pilipili.utils.FileU;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liyv
 * @since 2023-12-01
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {

    @Autowired
    VideoMapper videoMapper ;
    @Autowired
    UserMapper userMapper ;

    @Override
    public R<Video> add(Video entry) {

        return null;
    }

    @Override
    public R<List<Video>> selectByT(int tid, int page, int size) {
        page = page == 0 ? page:page*size ;
        Page<Video> p = new Page<>(page, size);
        QueryWrapper<Video> qw = new QueryWrapper<>();
        qw.eq("tid", tid).eq("status",2);
        IPage<Video> result = videoMapper.selectPage(p, qw);
        return R.ok( result.getRecords()) ;
    }

    @Override
    public R<VInfo> getVInfoByVid(int vid) {
        VInfo vInfo = videoMapper.selectInfoById(vid) ;
        vInfo.setUInfo(userMapper.selectInfoById(vInfo.getUid()));
        return R.ok(vInfo);
    }

    @Override
    public R<List<Video>> getsByULove(int uid) {

        return R.ok(videoMapper.selectInfosByULove(uid));
    }

    @Override
    public R<List<Video>> getsByUCollection(int uid) {
        return R.ok(videoMapper.selectInfosByUCollection(uid));
    }

    @Override
    public R<List<Video>> selectByLike(int tid, int page, int size, String likes) {
        page = page == 0 ? page:page*size ;
        Page<Video> p = new Page<>(page, size);
        QueryWrapper<Video> qw = new QueryWrapper<>();
        qw.eq("tid", tid).eq("status",2).like("title",likes);
        IPage<Video> result = videoMapper.selectPage(p, qw);
        return R.ok( result.getRecords()) ;
    }

    @Override
    public R<Video> update(Video entry) {
        Video video = videoMapper.selectById(entry.getId()) ;
        if (video == null) {
            return R.failed("不存在！请重新操作");
        }

        // del old
        if(entry.getIUrl() != null){
            FileU.deleteFile(video.getIUrl());
        } else { // 不改
            entry.setIUrl(video.getIUrl());
        }
        if(entry.getVUrl() != null){
            FileU.deleteFile(video.getVUrl());
        } else {
            entry.setVUrl(video.getVUrl());
        }
        entry.setUid(video.getUid());
        entry.setTime(video.getTime());
        videoMapper.updateById(entry) ;
        return R.ok(entry);
    }

    @Override
    public R<Video> addClick(int vid) {
        Video video = videoMapper.selectById(vid) ;
        video.setClick(video.getClick()+1);
        videoMapper.updateById(video) ;
        return R.ok(video) ;
    }

    @Override
    public R<Video> changeStatus(int vid, int newId ,String note) {
        Video v = videoMapper.selectById(vid) ;
        v.setStatus(newId);
        if (note != null) {
            v.setNote(note);
        }
        videoMapper.updateById(v) ;
        return R.ok(v);
    }

    @Override
    public R<List<Video>> getsByUidSid(int uid, int status) {
        QueryWrapper<Video> qw = new QueryWrapper<>() ;
        qw.eq("uid",uid).eq("status",status) ;

        return R.ok(videoMapper.selectList(qw));
    }

    @Override
    public R<List<VInfo>> getsWaitForReview() {
        List<VInfo> infos = videoMapper.selectInfos() ;
        for (VInfo item:infos){
            item.setUInfo(userMapper.selectInfoById(item.getUid()));
        }
        return R.ok(infos);
    }

    @Override
    public R<Video> changeStatus(Video video) {

        Video v = videoMapper.selectById(video.getId());
        v.setStatus(video.getStatus());
        v.setNote(video.getNote());
        videoMapper.updateById(v) ;
        return R.ok(v);
    }


}
